﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Truron.Purchase.Interface;
using Truron.Purchase.Api.Utility;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Truron.Purchase.Model;
using Newtonsoft.Json;
using Truron.Purchase.Utility;
using Newtonsoft.Json.Linq;

namespace Truron.Purchase.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class Purchase_SupplyController : ControllerBase
    {
        private readonly IPurchase_SupplyService _purchase_SupplyService;
        public Purchase_SupplyController(IPurchase_SupplyService Purchase_SupplyService)
        {
            _purchase_SupplyService = Purchase_SupplyService;
        }
        [HttpPost]
        public IActionResult GetList(LayPageModel param)
        {
            var userToken = SysCenterServiceHelp.GetUser(HttpContext);
            int pageSize = Convert.ToInt32(param.limit), pageIndex = Convert.ToInt32(param.page);
            var Tuple = _purchase_SupplyService.GetList(param.searchKey,param.searchJson, pageSize, pageIndex);
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { data = Tuple.list, count = Tuple.count }));
            //return null;
        }
        [HttpPost]
        public IActionResult Save(RecParam param)
        {
            Purchase_Supply entity = JsonConvert.DeserializeObject<Purchase_Supply>(param.strEntity);
            var userToken = SysCenterServiceHelp.GetUser(HttpContext);
            int resultCode = _purchase_SupplyService.Save(entity, userToken);
            return Ok(JsonConvert.SerializeObject(new ResultData(resultCode)));
        }
        public class RecParam
        {
            public string strEntity { get; set; }
        }
        [HttpPost]
        public IActionResult Del(RecParam param)
        {
            if (param.strEntity.Length == 0)
                return Ok(JsonConvert.SerializeObject(new ResultData(ErrorCodeConst.ParameterError)));
            List<int> ids = JsonConvert.DeserializeObject<List<int>>(param.strEntity);
            var userToken = SysCenterServiceHelp.GetUser(HttpContext);
            var resultCode = _purchase_SupplyService.Del(ids, userToken);
            return Ok(JsonConvert.SerializeObject(new ResultData(resultCode)));
        }
        [HttpPost]
        public IActionResult FindMaxNo(RecParam param)
        {
            var queryParam = JObject.Parse(param.strEntity.Replace("&nbsp;", ""));
            int no = _purchase_SupplyService.FindMaxNo((int)queryParam["cityId"], (int)queryParam["Id"]);
            return Ok(JsonConvert.SerializeObject(new ResultData(no)));
        }
        [HttpPost]
        public IActionResult GetALlList()
        {
            var list = _purchase_SupplyService.GetAllList();
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = 0,data = list }));
        }
        
    }
}
